By using the methods such as processDeposit()
and processCheck()
that have been
written for that purpose.
A class with private data provides access to that data
through
access methods.
An access method is a method
which uses the private data of its object
and is visible to other classes.
Some access methods alter data;
others return a value
but don't alter data.
Here is a main()
CheckingAccount
object.
class CheckingAccount { private String accountNumber; private String accountHolder; private int balance; . . . . } class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 ); System.out.println( bobsAccount.getBalance() ); bobsAccount.processDeposit( 200 ); System.out.println( bobsAccount.getBalance() ); } }
This main()
private
.
That is because main()
CheckingAccount
object.
This is the correct way to use an object.
But the previous version of the object could have been used incorrectly.
The idea of private
is to enforce correct use.
To run this program,
copy the CheckingAccount
class
from the previous page into a file CheckingAccount.java
.
Then copy
the
CheckingAccountTester
class
on this page into a file CheckingAccountTester.java
in the same
disk directory.
Then enter the commands
C:\chap33\> javac CheckingAccount.java CheckingAccountTester.java C:\chap33\> java CheckingAccountTester
Remember that the name of the file for the java
command
is the one that contains main()
What access methods are used in main()
?